package cn.gpdi.framework.sys;

import cn.gpdi.framework.util.CommUtil;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;
import java.io.IOException;

/**
 * Created by 司徒鑫(QQ:156845600) on 2018/4/17.
 */
public class SysListener extends HttpServlet implements ServletContextListener {

    //org.apache.log4j.Logger 在WEB启动之前不支持使用，因此日志打印失败。

    public void contextDestroyed(ServletContextEvent sce) {
        //用于在容器关闭时,操作
    }

    //用于在容器开启时,操作
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("项目初始化...");
        String proEvn;
        if ("dev".equals(System.getenv("project_evn"))||1>0) {
            System.out.println("当前环境：开发环境...");
            proEvn = "dev-env";
        } else {
            System.out.println("当前环境：生产环境...");
            proEvn = "pro-env";
        }

        //加载配置文件 project.properties
        System.out.println("加载配置文件 project.properties");
        try {
            CommUtil.getFile(proEvn + "\\" + "project.properties");
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(0);
        }

        System.out.println("是否开启会话校验 project.properties");
        try {
            if ("1".equals(CommUtil.getPropVal("LOGIN_CHECK"))) {

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        ServletContext sc = sce.getServletContext();
        //日志
        sc.setInitParameter("log4jConfigLocation", "classpath:" + proEvn + "/log4j.properties");
        //数据库
        sc.setInitParameter("jdbcConfigLocation", "classpath:" + proEvn + "/jdbc.properties");

    }
}